package models

import (
	"time"

	"github.com/google/uuid"
)

// Card 卡片数据模型
type Card struct {
	ID         uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	UUID       string    `json:"uuid" gorm:"uniqueIndex;not null"`
	Title      string    `json:"title" gorm:"not null"`
	FrontImage string    `json:"front_image" gorm:"not null"`
	BackText   string    `json:"back_text" gorm:"not null"`
	AudioPath  string    `json:"audio_path" gorm:"default:''"`
	AudioText  string    `json:"audio_text" gorm:"default:''"`
	CreatedAt  time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt  time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// CreateCardRequest 创建卡片请求
type CreateCardRequest struct {
	Title      string `json:"title" binding:"required"`
	FrontImage string `json:"front_image" binding:"required"`
	BackText   string `json:"back_text" binding:"required"`
	AudioPath  string `json:"audio_path"`
	AudioText  string `json:"audio_text"`
}

// UpdateCardRequest 更新卡片请求
type UpdateCardRequest struct {
	Title      string `json:"title" binding:"required"`
	FrontImage string `json:"front_image" binding:"required"`
	BackText   string `json:"back_text" binding:"required"`
	AudioPath  string `json:"audio_path"`
	AudioText  string `json:"audio_text"`
}

// APIResponse API 响应格式
type APIResponse struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
}

// HealthResponse 健康检查响应
type HealthResponse struct {
	Status     string `json:"status"`
	Service    string `json:"service"`
	Platform   string `json:"platform"`
	Time       string `json:"time"`
	CardsCount int    `json:"cards_count"`
}

// SyncResponse 同步状态响应
type SyncResponse struct {
	DeviceID   string `json:"device_id"`
	SyncTime   string `json:"sync_time"`
	CardsCount int    `json:"cards_count"`
}

// NewCard 创建新的卡片实例
func NewCard(title, frontImage, backText string) *Card {
	// 使用google/uuid库生成标准UUID
	cardUUID := uuid.New().String()
	return &Card{
		Title:      title,
		FrontImage: frontImage,
		BackText:   backText,
		UUID:       cardUUID,
		AudioPath:  "",
		AudioText:  "",
	}
}

// Update 更新卡片信息
func (c *Card) Update(title, frontImage, backText string) {
	c.Title = title
	c.FrontImage = frontImage
	c.BackText = backText
	// UUID保持不变，不更新
}

// UpdateWithAudio 更新卡片信息（包含音频）
func (c *Card) UpdateWithAudio(title, frontImage, backText, audioPath, audioText string) {
	c.Title = title
	c.FrontImage = frontImage
	c.BackText = backText
	c.AudioPath = audioPath
	c.AudioText = audioText
	// UUID保持不变，不更新
}
